<template>
  <!-- 登记记录 -->
  <section>
    <el-form :inline="true" @submit.native.prevent="getData(-1)">
      <el-form-item>
        <deptSearch v-model="form.deptId" style="width:250px;"></deptSearch>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="form.gmtCreateStart" value-format="yyyy-MM-dd" type="date" placeholder="登记开始日期"> </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="form.gmtCreateEnd" value-format="yyyy-MM-dd" type="date" placeholder="登记结束日期"> </el-date-picker>
      </el-form-item>
      <el-form-item>
        <patientSearch v-model="form.patientId"></patientSearch>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.status" clearable placeholder="预约状态">
          <template v-for="(item, index) in triageStatusArray">
            <el-option v-if="item.id != 0" :key="index" :value="item.id" :label="item.desc"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" native-type="submit" circle></el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="onExcel">导出Excel</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <el-col v-for="(item, index) in tableList" :key="index" :xs="12" :sm="12" :md="12" :lg="8" :xl="6" class="reserve-user-list">
        <div :class="['card-box', classToName(item.status)]">
          <div class="card-nr">
            <div>
              <img v-if="!item.patientInfo.head" src="@/img/unknown-avatar.png" class="head-img" alt="" style="width:70px;height:70px;" />
              <img v-else :src="item.patientInfo.head | imageUrl" class="head-img" alt="" style="width:70px;height:70px;" />
            </div>
            <div style="overflow: hidden;">
              <p class="overflow-omit">
                <span style="font-size:18px;">{{ item.patientInfo.name }}</span>
                &nbsp;
                <span
                  >（{{ item.patientInfo.sex | toSex }}&nbsp;<span v-if="item.patientInfo.age > 17">{{ item.patientInfo.age }}岁</span
                  ><span v-else>{{ item.patientInfo.birthday | toTimeContrast }}</span
                  >）</span
                >
              </p>
              <div class="overflow-omit">接诊：{{ item.doctorName }}，{{ item.deptName }}，{{ item.registerName }}</div>
              <div class="overflow-omit">预约时间：{{ item.dateDay }}</div>
              <div class="overflow-omit">登记人员：{{ item.hpMember ? item.hpMember.memberName : "" }}</div>
              <div class="overflow-omit">登记时间：{{ item.gmtCreate }}</div>
            </div>
          </div>
          <div class="card-footer">
            <div @click="onToUserDetail(item)">查看</div>
            <!-- <div v-if="item.status==0||item.status==1" @click="onDel(item,index)">删除</div> -->
            <div @click="onPrint(item)">打印标签</div>
            <!-- <div @click="onOpenInfo(item)">完善信息</div> -->
            <div>
              <el-dropdown trigger="click" style="width:100%;" @command="onMoreOperations($event, item, index)">
                <div>更多操作</div>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-if="item.status == 0 || item.status == 1" command="del">
                    <i class="el-icon-delete vertical-middle"></i>
                    <span class="vertical-middle">删除</span>
                  </el-dropdown-item>
                  <el-dropdown-item command="wsxx">
                    <i class="el-icon-edit-outline vertical-middle"></i>
                    <span class="vertical-middle">完善信息</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <div class="zt-box">{{ item.status | changeAssist(triageStatusArray) }}</div>
          <div v-if="item.patientInfo.levelId" class="vip-box">
            <el-tooltip :content="item.patientInfo.levelName" placement="top">
              <img src="@/img/icon-vip.png" alt="" />
            </el-tooltip>
          </div>
        </div>
      </el-col>
    </el-row>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <dialogWriteData v-model="dialogWriteData" :patientId="curItem.patientInfo.patientId"></dialogWriteData>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/patientSign.html"></iframe>
  </section>
</template>
<script>
import Excel from "@/_js/excel.js";
export default {
  props: ["qxBtns"],
  components: {
    patientSearch: () => import("@/pages/_com/patientSearch.vue"),
    dialogWriteData: () => import("@/pages/_com/dialogWriteData.vue"),
    deptSearch: () => import("@/pages/_com/deptSearch.vue")
  },
  data() {
    return {
      dialogWriteData: false,
      curItem: {
        patientInfo: {
          patientId: ""
        }
      },
      form: {
        patientId: "",
        deptId: "",
        gmtCreateStart: "",
        gmtCreateEnd: "",
        status: ""
      },
      pager: { pageSize: 12, pageNum: 1, total: 0 },
      tableList: [],
      //预约状态
      triageStatusArray: []
    };
  },
  mounted() {
    let th = this;
    th.getCommonConfigByKey("预约状态").then(res => {
      th.triageStatusArray = res;
    });
    const date = new Date();
    th.form.gmtCreateStart = th.$utils.FormatTime("yyyy-MM-dd", date.getTime());
    th.getData();
  },
  methods: {
    onMoreOperations(key, obj, index) {
      const th = this;
      switch (key) {
        case "del":
          th.onDel(obj, index);
          break;
        case "wsxx":
          th.onOpenInfo(obj);
          break;
      }
    },
    //打开完善信息
    onOpenInfo(obj) {
      const th = this;
      th.curItem = obj;
      th.dialogWriteData = true;
    },
    onExcel() {
      let th = this;
      let j = {
        pageSize: 5000,
        pageNum: 1
      };
      Object.assign(j, th.form);
      th.showLoading();
      th.$api
        .getRegisterListInfo(j)
        .then(res => {
          if (res.executed) {
            let data = res.dataBody.list;
            let title = [
              { key: "name", name: "姓名", child: "patientInfo" },
              {
                key: "sex",
                name: "性别",
                child: "patientInfo",
                type: "enum",
                enum: { 1: "男", 2: "女" }
              },
              { key: "age", name: "年龄", child: "patientInfo" },
              { key: "registerName", name: "接诊类型" },
              { key: "deptName", name: "登记科室" },
              { key: "doctorName", name: "接诊医生" },
              { key: "memberName", name: "登记人员", child: "hpMember" },
              { key: "gmtCreate", name: "登记时间" }
            ];
            let titleItem = {
              key: "status",
              name: "状态",
              type: "enum",
              enum: {}
            };
            th.triageStatusArray.forEach(d => {
              titleItem.enum[d.id] = d.desc;
            });
            // 2021 feat by jy
            // 增加 预约内容
            title.push(titleItem, { key: "registerContent", name: "预约内容" });
            Excel(data, title, "患者登记记录");
          }
          th.showLoading(false);
        })
        .catch(res => {
          th.showLoading(false);
        });
    },
    onPrint(obj) {
      let th = this;
      let json = JSON.parse(JSON.stringify(obj.patientInfo));
      json.barCode = th.$config.imageUrl + json.barCode;
      th.$refs["iframe1"].contentWindow.postMessage(json, `${window.location.origin}/print/patientSign.html`);
    },
    onToUserDetail(obj) {
      let th = this;
      if (th.verifyUrl("/page/patientList")) {
        let url = "/page/userDetail";
        let j = {
          id: th.jiaMi(obj.patientInfo.patientId)
        };
        th.addNavMenu("病人库", url, "userDetail");
        th.$router.push({
          path: url,
          query: th.getUrlToken(j)
        });
      }
    },
    onDel(obj, index) {
      let th = this;
      if (!th.qxBtns.del) {
        th.$message.warning("权限不足");
        return;
      }
      th.$confirm(`确定删除？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          let j = {
            id: obj.id,
            cancelDesc: ""
          };
          th.showLoading();
          th.$api.registerCancelOrder(j).then(res => {
            if (res.executed) {
              th.$message.success("操作成功");
              obj.status = 4;
            } else {
              th.$message.error(res.message);
            }
            th.showLoading(false);
          });
        })
        .catch(() => {});
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      // th.form.gmtCreateEnd = "";
      // if (th.form.gmtCreateStart) th.form.gmtCreateEnd = th.form.gmtCreateStart;
      th.showLoading();
      let j = {};
      Object.assign(j, th.form, th.pager);
      th.$api.getRegisterListInfo(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
        th.showLoading(false);
      });
    },
    //判断类名
    classToName(type) {
      switch (type) {
        case 1:
          return "bg-yjz";
          break;
        case 2:
          return "bg-sy";
          break;
        case 3:
          return "bg-hong";
          break;
        case 4:
          return "bg-yqx";
          break;
        case 5:
          return "bg-jzz";
          break;
        default:
          return "bg-dfz";
          break;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../../_css/reserve.scss";
</style>
